package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.example.lease.model.entities.RoomInfo;
import org.example.lease.model.enums.ReleaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.RoomInfoService;
import org.example.lease.vo.room.RoomDetailVo;
import org.example.lease.vo.room.RoomItemVo;
import org.example.lease.vo.room.RoomQueryVo;
import org.example.lease.vo.room.RoomSubmitVo;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/18 9:21
 */
@RestController
@Slf4j
@Tag(name = "房间管理")
@RequestMapping("/admin/room")
public class RoomController {
    @Resource
    private RoomInfoService roomInfoService;

    @Operation(summary = "更新或者添加房间信息")
    @PostMapping("/saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody RoomSubmitVo roomSubmitVo) {
        roomInfoService.saveOrUpdateRoomSubmitVo(roomSubmitVo);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询房间列表")
    @GetMapping("/pageItem")
    public Result<IPage<RoomItemVo>> pageItem(@RequestParam Long current,
                                              @RequestParam Long size,
                                              RoomQueryVo queryVo) {
        IPage<RoomItemVo> page = new Page<>(current, size);
        IPage<RoomItemVo> roomItemVoIPage = roomInfoService.pageRoomItemVo(page, queryVo);
        return Result.ok(roomItemVoIPage);
    }

    @Operation(summary = "根据id来查询房间详细信息")
    @GetMapping("/getDetailById")
    public Result<RoomDetailVo> getDetailById(@RequestParam Long id) {
        RoomDetailVo roomDetailVo = roomInfoService.getRoomDetailVoByRoomId(id);
        return Result.ok(roomDetailVo);
    }

    @Operation(summary = "根据id删除房间信息")
    @DeleteMapping("/removeById")
    public Result<?> removeRoomById(@RequestParam Long id) {
        roomInfoService.removeRoomById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id更新房间的发布信息")
    @PostMapping("/updateReleaseStatusById")
    public Result<?> updateStatus(@RequestParam Long id, @RequestParam ReleaseStatus status) {
        roomInfoService.update(Wrappers.lambdaUpdate(RoomInfo.class)
                .eq(RoomInfo::getId, id).set(RoomInfo::getIsRelease, status));
        return Result.ok();
    }

    @Operation(summary = "根据id查询房间列表")
    @GetMapping("/listBasicByApartmentId")
    public Result<?> listBasicByApartmentId(@RequestParam Long id) {
        List<RoomInfo> roomInfoList = roomInfoService.list(Wrappers.lambdaQuery(RoomInfo.class)
                .eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED)
                .eq(RoomInfo::getApartmentId, id));
        return Result.ok(roomInfoList);
    }

}
